package com.fkcai.hansoul.model.vo.post;

import com.fkcai.hansoul.model.entity.PostComment;
import com.fkcai.hansoul.model.entity.User;
import lombok.Data;
import lombok.experimental.Accessors;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 评论 vo
 */
@Data
@Accessors(chain = true) // 支持链式调用
public class PostCommentVo {
	private Integer commentId;  // 评论ID
	private String content;     // 评论内容
	private Integer likeCount;  // 评论点赞数
	private Integer replyCount; // 评论回复数
	private LocalDateTime createTime;   // 评论创建时间
	
	private UserInfo author;      // 评论者信息
	private List<PostCommentReplyVo> replies; // 回复列表
	
	/**
	 * 嵌套用户信息
	 */
	@Data
	public static class UserInfo {
		private Integer userId;     // id
		private String username;    // 用户名
		private String avatarUrl;   // 头像URL
		private Integer userLevel;      // 等级
		
		public static UserInfo fromUser (User user) {
			UserInfo userInfo = new UserInfo();
			
			userInfo.setUserId(user.getUserId());
			userInfo.setUsername(user.getUsername());
			userInfo.setAvatarUrl(user.getAvatarUrl());
			userInfo.setUserLevel(user.getUserLevel());
			
			return userInfo;
		}
	}
	
	// 评论
	public static PostCommentVo fromPostComment (PostComment postComment, User author) {
		PostCommentVo postCommentVo = new PostCommentVo();
		
		postCommentVo.setCommentId(postComment.getCommentId());
		postCommentVo.setContent(postComment.getContent());
		postCommentVo.setLikeCount(postComment.getLikeCount());
		postCommentVo.setReplyCount(postComment.getReplyCount());
		postCommentVo.setCreateTime(postComment.getCreateTime());
		
		if (author != null) {
			postCommentVo.setAuthor(UserInfo.fromUser(author));
		}
		
		return postCommentVo;
	}
	
	
	/**
	 * 添加回复列表
	 */
	public PostCommentVo withReplies(List<PostCommentReplyVo> replies) {
		this.replies = replies;
		return this;
	}
}
